package cn.edu.heuet.bnve.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.databinding.ItemListClassBinding;
import cn.edu.heuet.bnve.view.group.GroupRecyclerAdapter;
import cn.edu.heuet.bnve.model.NEUClass;
import cn.edu.heuet.bnve.view.RoundTextView;

public class ClassAdapter extends GroupRecyclerAdapter<String, NEUClass> {



    private  List<NEUClass> classes;
    private Context context;
    private Map<String, Integer> colorMap = new HashMap<>();

    @RequiresApi(api = Build.VERSION_CODES.N)
    public ClassAdapter(Context context, List<NEUClass> classes) {
        super(context);
        this.context = context;
        this.classes=classes;
        hadleData();
        LinkedHashMap<String, List<NEUClass>> map = new LinkedHashMap<>();
        List<String> titles = new ArrayList<>();
        int count =this.classes.size()-1;
        map.put("今日课程", this.classes);
        titles.add("今日课程");

        resetGroups(map,titles);
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder holder, NEUClass item, int position) {
        if(item.getDay()>0) {
            ClassAdapter.ClassViewHolder h = (ClassAdapter.ClassViewHolder) holder;
            h.mTextName.setText(item.getName());
            h.mTextPosition.setText(item.getPosition());
            h.mTextTeacher.setText(item.getTeacher());
            h.mTextSection.setText(item.getSection() + "~" + (item.getSection() + 1) + "节");
            h.imageView.addView(new RoundTextView(context,25,getColor(colorMap, item.getName())));
        }
    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        if (type==0){
            return new ClassAdapter.ClassViewHolder(mInflater.inflate(R.layout.item_list_class, parent, false));
        }
        else if(type==2){
            return new ClassAdapter.BottomViewHolder(mInflater.inflate(R.layout.item_list_bottom, parent, false));
        }
        else{
            return new ClassAdapter.BlankViewHolder(mInflater.inflate(R.layout.item_list_no_class, parent, false));
        }
    }
    @Override
    public int getItemViewType(int position) {
        if (classes.get(position).getDay()==-1) {
            return 1;
        } else if(classes.get(position).getDay()==0){
            return 2;
        }
        else{
            return 0;
        }
    }

    private static class ClassViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextName, mTextSection,mTextPosition,mTextTeacher;
        private FrameLayout imageView;

        @SuppressLint("ResourceAsColor")
        private ClassViewHolder(View itemView) {
            super(itemView);
            ItemListClassBinding binding = DataBindingUtil.bind(itemView);
            mTextName = binding.txName;
            mTextSection = binding.txScheduleSection;
            mTextPosition = binding.txSchedulePosition;
            mTextTeacher = binding.txScheduleTeacher;
            imageView = binding.imageView;

        }

    }

    private static class BlankViewHolder extends RecyclerView.ViewHolder {
        @SuppressLint("ResourceAsColor")
        private BlankViewHolder(View itemView) {
            super(itemView);
        }
    }

    private static class BottomViewHolder extends RecyclerView.ViewHolder {
        @SuppressLint("ResourceAsColor")
        private BottomViewHolder(View itemView) {
            super(itemView);
        }
    }

    private int getColor(Map<String, Integer> map, String name) {
        Integer tip = map.get(name);
        if (null != tip) {
            return tip;
        } else {
            int i = context.getResources().getColor(color[map.size() % color.length]);
            map.put(name, i);
            return i;
        }
    }

    private int color[] = {
            R.color.one, R.color.two, R.color.three,
            R.color.four, R.color.five, R.color.six,
            R.color.seven, R.color.eight, R.color.nine,
            R.color.ten, R.color.eleven, R.color.twelve,
            R.color.thirteen, R.color.fourteen, R.color.fifteen
    };

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void hadleData(){
        if(classes.size()==0){
            NEUClass neuClass = new NEUClass();
            neuClass.setDay(-1);
            classes.add(neuClass);
        }
        else{
            NEUClass neuClass = new NEUClass();
            neuClass.setDay(0);
            classes.add(neuClass);
        }
//        classes.sort(Comparator.comparing(NEUClass::getSection));
    }
}
